#include <sstream>
#include "ice/candidate.h"
namespace xrtc
{

    uint32_t Candidate::GetPriority(uint32_t type_preference,
                                     int network_adapter_preference,
                                     int relay_preference)
    {

        int addr_ref = rtc::IPAddressPrecedence(address.ipaddr());
        int local_pref = ((network_adapter_preference << 8) | (addr_ref)) + relay_preference;
        /*
            计算公式：
            priority = (2^24) *(type preference)+(2^8) *(local preference) +(2^0)(256-component ID)
        */
        return (type_preference << 24) | (local_pref << 8) | (256 - (int)component);
    }

    std::string Candidate::ToString() const
    {
        std::stringstream ss;
        ss << "Cand[" << foundation << ":" << (int)component << ":" << protocol << ":" 
        << priority << ":" << address.ToString() << ":" << type << ":" << username << ":" << password<<"]";
        return ss.str();
    }

} // namespace xrtc